﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllegroWebAPI
{
    public class UserInfo
    {
        public class FeedbackPoints
        {

        }

        public class SellRatingAvarage
        {

        }

        public long Id { get; set; }
        public string Name { get; set; }
        public int Country { get; set; }
        public DateTime CreateDate { get; set; }
        public DateTime LoginDate { get; set; }
        /// <summary>
        /// Liczba punktów użytkownika.
        /// </summary>
        public int Rating { get; set; }
        public bool IsNewUser { get; set; }
        public bool NotActivated { get; set; }
        public bool Closed { get; set; }
        public bool Blocked { get; set; }
        public bool Terminated { get; set; }
        public bool HasPage { get; set; }
        public bool IsSuperSeller { get; set; }
        public bool IsEco { get; set; }
        public FeedbackPoints PositiveFeedback { get; set; }
        public FeedbackPoints NegativeFeedback { get; set; }
        public FeedbackPoints NeutralFeedback { get; set; }

        public bool JuniorStatus { get; set; }
        public bool HasShop { get; set; }
        public bool CompanyIcon { get; set; }
        public int SellRatingCount { get; set; }

        public SellRatingAvarage SellRating { get; set; }
        public bool AllegroStandard { get; set; }
        public bool IsbTwoCSeller { get; set; }

    }

    public class AllegroUser
    {
        AllegroClient _api;
        long _userId = 0;
        string _userName = "";

        public string Name { get { if (string.IsNullOrEmpty(_userName)) { _userName = NameById(_userId); } return _userName; } }
        public long Id { get { if (_userId == 0) { _userId = IdByName(_userName); } return _userId; } }

        public UserInfo Info
        {
            get { 
                if (_userId == 0) 
                    return this.doShowUser(_api.WebApiKey, _api.CountryId, 0, _userName); 
                else 
                    return this.doShowUser(_api.WebApiKey, _api.CountryId, (int)_userId, ""); 
            }
        }

        public string MyPage { 
            get {
                string alcoLink, pharmLink;
                return _api.WebService.doShowUserPage(_api.WebApiKey, _api.CountryId, Id, out pharmLink, out alcoLink);
            } 
        }

        public List<UserItemList> GetItems(int offset, int limit, out int itemCount)
        {
            int itmsCnt = 0;
            List<UserItemList> lul = new List<UserItemList>();
            UserItemList[] re = _api.WebService.doGetUserItems((int)Id, _api.WebApiKey, _api.CountryId, offset, limit, out itmsCnt);
            itemCount = itmsCnt;
            lul.AddRange(re);
            return lul;
        }

        public List<UserItemList> GetItems()
        {
            int itmsCnt = 0;
            int page = 0;
            List<UserItemList> lul = new List<UserItemList>();

            lul.AddRange(GetItems(page, 100, out itmsCnt));
            itmsCnt = itmsCnt - 100;
            while (itmsCnt > 0)
            {
                page++;
                lul.AddRange(GetItems(page, 100, out itmsCnt));
                itmsCnt = itmsCnt - 100;
            }

            return lul;
        }

        public AllegroUser(AllegroClient api, long userId)
        {
            _api = api;
            _userId = userId;
        }

        public AllegroUser(AllegroClient api, string userName)
        {
            _api = api;
            _userName = userName;
        }

        private UserInfo doShowUser(string WebApiKey, int CountryId, long userId, string userLogin)
        {
            long usercreatedate, userlogindate;
            int userrating, userisnewuser, usernotactivated, userclosed, userblocked, userterminated, userhaspage, userissseller, useriseco;
            int userjuniorstatus, userhasshop, usercompanyicon, usersellratingcount, userisallegrostandard, userisb2cseller;
            ShowUserFeedbacks suf1 = new ShowUserFeedbacks();
            ShowUserFeedbacks suf2 = new ShowUserFeedbacks();
            ShowUserFeedbacks suf3 = new ShowUserFeedbacks();
            SellRatingAverageStruct[] a = new SellRatingAverageStruct[1];

            _api.WebService.doShowUser(WebApiKey, CountryId, ref userId, ref userLogin, out usercreatedate, out userlogindate, out userrating, out userisnewuser, out usernotactivated, out userclosed,
                                       out userblocked, out userterminated, out userhaspage, out userissseller, out useriseco, out suf1, out suf2, out suf3,
                                       out userjuniorstatus, out userhasshop, out usercompanyicon, out usersellratingcount, out a, out userisallegrostandard, out userisb2cseller);
            
            return new UserInfo()
            {
                Id = userId,
                Name = userLogin,
                CreateDate = Convert.ToDateTime(usercreatedate),
                LoginDate = Convert.ToDateTime(userlogindate),
                Rating = userrating,
                IsNewUser = userisnewuser == 1,
                NotActivated = usernotactivated == 0,
                Closed = userclosed == 1,
                Blocked = userblocked == 1,
                Terminated = userterminated == 1,
                HasPage = userhaspage == 1,
                IsSuperSeller = userissseller == 1,
                IsEco = useriseco == 1,
                PositiveFeedback = new UserInfo.FeedbackPoints() { }, // suf1
                NegativeFeedback = new UserInfo.FeedbackPoints() { }, //suf2,
                NeutralFeedback = new UserInfo.FeedbackPoints() { }, //suf3,
                JuniorStatus = userjuniorstatus == 1,
                HasShop = userhasshop == 1,
                CompanyIcon = usercompanyicon == 1,
                SellRatingCount = usersellratingcount,
                SellRating = new UserInfo.SellRatingAvarage(){}, // a,
                AllegroStandard = userisallegrostandard == 1,
                IsbTwoCSeller = userisb2cseller == 1 
            };
        }

        private string NameById(long userId)
        {
            return _api.WebService.doGetUserLogin(_api.CountryId, (int)userId, _api.WebApiKey);
        }

        private int IdByName(string userLogin)
        {
            return _api.WebService.doGetUserID(_api.CountryId, userLogin, "", _api.WebApiKey);
        }

    }

}
